package com.bit.forum.controller;


import com.bit.forum.common.AppResult;
import com.bit.forum.common.ResultCode;
import com.bit.forum.config.APPConfig;
import com.bit.forum.model.Message;
import com.bit.forum.model.User;
import com.bit.forum.services.IMessageService;
import com.bit.forum.services.IUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/message")
@Api(tags = "站内信接口")
public class MessageController {

    @Resource
    private IMessageService messageService;

    @Resource
    private IUserService userService;

    @ApiOperation("发送站内信")
    @PostMapping("/send")
    public AppResult send(HttpServletRequest request,
                          @ApiParam("接收者id") @RequestParam("receiveUserId") @NonNull Long receiveUserId,
                          @ApiParam("内容") @RequestParam("content") @NonNull String content){
        //获取当前登录的用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(APPConfig.USER_SESSION);
        //判断用户是否禁言
        if (user.getState()==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //不能给自己发
        if (user.getId() == receiveUserId){
            return AppResult.failed("不能给自己发");
        }
        //接收者是否存在
        User receiveUser = userService.selectById(receiveUserId);
        if (receiveUser == null || receiveUser.getDeleteState() == 1){
            return AppResult.failed("接收者状态异常");
        }
        //封装对象
        Message message = new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(receiveUserId);
        message.setContent(content);
        //调用service
        messageService.create(message);
        //返回结果
        return AppResult.success("发送成功");
    }


    @ApiOperation("获取未读数量")
    @GetMapping("/getUnreadCount")
    public AppResult<Integer> getUnreadCount(HttpServletRequest request){
        //获取当前登录用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(APPConfig.USER_SESSION);
        //service
        Integer count = messageService.selectUnreadCount(user.getId());
        return AppResult.success(count);
    }


    @ApiOperation("获取站内信列表")
    @GetMapping("/getAll")
    public AppResult<List<Message>> getAll(HttpServletRequest request){
        //获取当前登录用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(APPConfig.USER_SESSION);
        //service
        List<Message> messages = messageService.selectByReceiveUserId(user.getId());
        return AppResult.success(messages);
    }


    @ApiOperation("更新状态为已读")
    @PostMapping("/markRead")
    public AppResult markRead(HttpServletRequest request,
                              @ApiParam("站内信ID") @RequestParam("id") @NonNull Long id){
        //校验站内信是否存在
        Message message = messageService.selectById(id);
        if (message == null || message.getDeleteState() ==1){
            return AppResult.failed("站内信不存在");
        }

        //是不是发送人
        //获取当前登录用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(APPConfig.USER_SESSION);
        if (message.getReceiveUserId() != user.getId()){
            return AppResult.failed(ResultCode.FAILED_FORBIDDEN);
        }
        //封装
        messageService.updateStateById(id, (byte) 1);
        return AppResult.success();
    }


    /***
     * 回复站内信
     * @param request
     * @param repliedId
     * @param content
     * @return
     */
    @ApiOperation("回复站内信")
    @PostMapping("/reply")
    public AppResult reply(HttpServletRequest request,
                           @ApiParam("要回复的站内信ID") @RequestParam("repliedId") @NonNull Long repliedId,
                           @ApiParam("内容") @RequestParam("content") @NonNull String content){
        //当前用户
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(APPConfig.USER_SESSION);
        if (user.getState() ==1){
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //站内信是否存在
        Message existMessage = messageService.selectById(repliedId);
        if (existMessage == null || existMessage.getDeleteState() == 1){
            return AppResult.failed(ResultCode.FAILED_MESSAGE_NOT_EXISTS);
        }
        //不能给自己回复
        if (user.getId() == existMessage.getPostUserId()){
            return AppResult.failed("不能给自己发送");
        }
        //构造对象
        Message message = new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(existMessage.getPostUserId());
        message.setContent(content);
        //SERVICE
        messageService.reply(repliedId,message);
        return AppResult.success();
    }
}
